package com.ruoyi.web.controller.shopping;

import com.ruoyi.system.domain.shopping.Address;
import com.ruoyi.system.service.shopping.AddressService;
import com.ruoyi.system.view.common.Result;
import com.ruoyi.web.controller.shopping.common.BaseContext;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/xiaomi/v2/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @PostMapping
    public Result insert(@RequestBody Address address){
        address.setUserId(BaseContext.getId());
        Boolean aBoolean = addressService.insertAddress(address);
        return Result.success(aBoolean,"添加成功");
    }
    @PutMapping
    public Result update(@RequestBody Address address){
        Integer userId = BaseContext.getId();
        address.setUserId(userId);
        Boolean aBoolean = addressService.updateAddress(address);
        return Result.success(aBoolean,"修改成功");
    }
    @GetMapping("/list")
    public Result<List<Address>> list(){
        Integer userId = BaseContext.getId();
        List<Address> addresses = addressService.selectList(userId);
        return Result.success(addresses,"查询成功");
    }

    @DeleteMapping("/{addressId}")
    public Result delete(@PathVariable Integer addressId){
        Integer userId = BaseContext.getId();
        Boolean aBoolean = addressService.deleteAddress(userId, addressId);
        return Result.success(aBoolean,"操作成功");
    }
}
